package com.lsh.oms.core.service.other;

import com.lsh.base.common.json.JsonUtils;
import com.lsh.oms.core.constant.ReturnCode;
import com.lsh.oms.core.dao.other.OrderShippingDetailMapper;
import com.lsh.oms.core.dao.other.OrderShippingHeadMapper;
import com.lsh.oms.core.dao.other.OrderSignHeadMapper;
import com.lsh.oms.core.enums.OrderStatus;
import com.lsh.oms.core.exception.BusinessException;
import com.lsh.oms.core.model.order.OrderShippingDetail;
import com.lsh.oms.core.model.order.OrderShippingHead;
import com.lsh.oms.core.model.order.OrderShippingHeadWithBLOBs;
import com.lsh.oms.core.model.order.OrderSignHead;
import com.lsh.oms.core.service.order.OrderUpdateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * @author peter
 */
@Service
public class ShippingOrderService {

    private static Logger log = LoggerFactory.getLogger(ShippingOrderService.class);

    @Autowired
    private OrderShippingDetailMapper shippingDetailDao;

    @Autowired
    private OrderShippingHeadMapper shippingHeadDao;

    @Autowired
    private OrderSignHeadMapper orderSignHeadMapper;

    @Autowired
    private OrderUpdateService orderUpdateService;


    @Transactional(rollbackFor = Exception.class)
    public boolean dealShipping(OrderShippingHeadWithBLOBs osh, List<OrderShippingDetail> details,OrderStatus orderStatus, String logSign) {
        Long orderCode = osh.getOrderId();
        int num;
        try {
            boolean updateDb = orderUpdateService.updateOrderStatus(osh.getOrderId(), orderStatus, osh.getLackFlag());
            if (!updateDb) {
                log.info(logSign + ",更新数据库修改订单状态失败,返回0,订单号:" + orderCode);
                return false;
            }

            num = addOrderShipping(osh, details, logSign);

        } catch (Exception e) {
            e.printStackTrace();
            log.error("发货单保存数据库失败,异常信息: " + e.getMessage(), e);
            throw new BusinessException(ReturnCode.REQUEST_FAIL, "发货单保存数据库失败dao");
        }
        return num == 1;
    }




    @Transactional(rollbackFor = Exception.class)
    public int addOrderShipping(OrderShippingHeadWithBLOBs osh, List<OrderShippingDetail> details, String logSign) {
        Long orderCode = osh.getOrderId();
        int num;
        try {
            OrderShippingHeadWithBLOBs bolbs = shippingHeadDao.selectByOrderCode(orderCode);
            if (bolbs == null) {
                log.info(logSign + ",osh:" + JsonUtils.obj2Json(osh));
                num = shippingHeadDao.insertSelective(osh);
                if (num == 0) {
                    return num;
                }
                log.info(logSign + "detail:" + JsonUtils.obj2Json(details));
                shippingDetailDao.saveBatch(details);
            } else {
                //目前只有发货，没有其他
                log.info(logSign + ",订单" + orderCode + ",已经存在发货单");
                num = 1;
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("发货单保存数据库失败,异常信息: " + e.getMessage(), e);
            throw new BusinessException(ReturnCode.REQUEST_FAIL, "发货单保存数据库失败dao");
        }
        return num;
    }

    public OrderShippingHeadWithBLOBs getBolbs(Long orderId) {
        return shippingHeadDao.selectByOrderCode(orderId);
    }

    @Transactional(rollbackFor = RuntimeException.class)
    public boolean updateStatus(OrderShippingHead osh) {
        return shippingHeadDao.updateStatusByShippingId(osh) >= 0;
    }

    public List<OrderShippingDetail> getDetails(Long shippingOrderId) {
        return shippingDetailDao.selectByShippingOrderId(shippingOrderId);
    }


    public List<OrderShippingHead> selectByProviderId(String providerId, boolean fillDetails, Integer maxSize) {

        List<OrderShippingHead> orderShippingHeads = this.shippingHeadDao.selectByProviderId(providerId, maxSize);

        if (CollectionUtils.isEmpty(orderShippingHeads)) {
            return Collections.EMPTY_LIST;
        }

        if (fillDetails) {
            for (OrderShippingHead orderShippingHead : orderShippingHeads) {

                orderShippingHead.setDetails(shippingDetailDao.selectByShippingOrderId(orderShippingHead.getShippingOrderId()));
            }
        }

        return orderShippingHeads;
    }

    public OrderShippingHead selectOneByOrderCode(Long orderCode, boolean fillDetails) {

        OrderShippingHead orderShippingHead = this.shippingHeadDao.selectByOrderCode(orderCode);

        if (orderShippingHead == null) {
            return null;
        }

        if (fillDetails) {

            orderShippingHead.setDetails(shippingDetailDao.selectByShippingOrderId(orderShippingHead.getShippingOrderId()));
        }

        return orderShippingHead;
    }

    public OrderShippingHeadWithBLOBs selectOneByShipCode(Long shipOrderCode, boolean fillDetails) {

        OrderShippingHeadWithBLOBs orderShippingHead = this.shippingHeadDao.selectByshippingOrderId(shipOrderCode);

        if (orderShippingHead == null) {
            return null;
        }

        if (fillDetails) {

            orderShippingHead.setDetails(shippingDetailDao.selectByShippingOrderId(orderShippingHead.getShippingOrderId()));
        }

        return orderShippingHead;
    }

    public int updateOmsExtByShippingId(OrderShippingHead record) {

        return this.shippingHeadDao.updateOmsExtByShippingId(record);
    }


    public int updateByPKSelective(OrderShippingHeadWithBLOBs record) {

        return this.shippingHeadDao.updateByPrimaryKeySelective(record);
    }

    public OrderSignHead selectAll(Long orderCode){

        return orderSignHeadMapper.selectByOrderCode(orderCode);
    }


}
